from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine,Column,String,Integer,ForeignKey,DATE
from sqlalchemy.orm import sessionmaker
from database.dataconn import connection

Base = declarative_base()

engine = create_engine(connection.MYSQL_URL)

session = sessionmaker(engine)()

class Employ(Base): #员工表
    __tablename__ = "employ"  # 数据表名
    Eno = Column(Integer, primary_key=True)
    Ename = Column(String)
    Epassword = Column(String)
    Elevel = Column(String)
    Ephone = Column(String)
    Epay = Column(Integer)
    Eremarks = Column(String)

    def to_dict(self):
        model_dict = dict(self.__dict__)
        del model_dict['_sa_instance_state']
        return model_dict
    Base.to_dict = to_dict

    def single_to_sict(self):
        return {c.name: getattr(self, c.name) for c in self._table_.columns}

    def double_to_dict(self):
        result = {}
        for key in self.__mapper__.c.keys():
            if getattr(self,key) is not None:
                result[key] = str(getattr(self,key))
            else:
                result[key] = getattr(self,key)
        return result
    def to_json(all_vendors):
        v = [ven.double_to_dict() for ven in all_vendors]
        return v

class Member(Base):#会员表
    __tablename__ = "member"  # 数据表名
    Mcardnum = Column(Integer, primary_key=True)
    Mname = Column(String)
    Mphone = Column(String)
    Mpoint = Column(Integer)

    def to_dict(self):
        model_dict = dict(self.__dict__)
        del model_dict['_sa_instance_state']
        return model_dict
    Base.to_dict = to_dict

    def single_to_sict(self):
        return {c.name: getattr(self, c.name) for c in self._table_.columns}

    def double_to_dict(self):
        result = {}
        for key in self.__mapper__.c.keys():
            if getattr(self,key) is not None:
                result[key] = str(getattr(self,key))
            else:
                result[key] = getattr(self,key)
        return result
    def to_json(all_vendors):
        v = [ven.double_to_dict() for ven in all_vendors]
        return v

class Supplier(Base): #供应商表
    __tablename__ = "supplier"  # 数据表名
    Sno = Column(Integer, primary_key=True)
    Sallname = Column(String)
    Sname = Column(String)
    Saddress = Column(String)
    Sphone = Column(String)
    Semail = Column(String)
    Scontact = Column(String)
    Sconphone = Column(String)
    Sremarks = Column(String)

    def to_dict(self):
        model_dict = dict(self.__dict__)
        del model_dict['_sa_instance_state']
        return model_dict
    Base.to_dict = to_dict

    def single_to_sict(self):
        return {c.name: getattr(self, c.name) for c in self._table_.columns}

    def double_to_dict(self):
        result = {}
        for key in self.__mapper__.c.keys():
            if getattr(self,key) is not None:
                result[key] = str(getattr(self,key))
            else:
                result[key] = getattr(self,key)
        return result
    def to_json(all_vendors):
        v = [ven.double_to_dict() for ven in all_vendors]
        return v

class Commodity(Base):#商品表
    __tablename__ = "commodity"  # 数据表名
    Cno = Column(Integer, primary_key=True)
    Cname = Column(String)
    Cunitprice = Column(Integer)
    Sno = Column(Integer, ForeignKey("supplier.Sno"))
    Cdescribe = Column(String)
    Cremarks = Column(String)

    def to_dict(self):
        model_dict = dict(self.__dict__)
        del model_dict['_sa_instance_state']
        return model_dict
    Base.to_dict = to_dict

    def single_to_sict(self):
        return {c.name: getattr(self, c.name) for c in self._table_.columns}

    def double_to_dict(self):
        result = {}
        for key in self.__mapper__.c.keys():
            if getattr(self,key) is not None:
                result[key] = str(getattr(self,key))
            else:
                result[key] = getattr(self,key)
        return result
    def to_json(all_vendors):
        v = [ven.double_to_dict() for ven in all_vendors]
        return v

class Purchases(Base):#采购表
    __tablename__ = "purchases"  # 数据表名
    Pno = Column(Integer, primary_key=True) #清单号
    Eno = Column(Integer,ForeignKey("employ.Eno"))
    Cno = Column(Integer, ForeignKey("commodity.Cno"))
    Pnum = Column(Integer)
    Pdate = Column(DATE)
    Premarks = Column(String)

    def to_dict(self):
        model_dict = dict(self.__dict__)
        del model_dict['_sa_instance_state']
        return model_dict
    Base.to_dict = to_dict

    def single_to_sict(self):
        return {c.name: getattr(self, c.name) for c in self._table_.columns}

    def double_to_dict(self):
        result = {}
        for key in self.__mapper__.c.keys():
            if getattr(self,key) is not None:
                result[key] = str(getattr(self,key))
            else:
                result[key] = getattr(self,key)
        return result
    def to_json(all_vendors):
        v = [ven.double_to_dict() for ven in all_vendors]
        return v

